package com.tptech.nearme.data;

import javax.inject.Inject;
import javax.persistence.EntityManager;

public class BaseRepository<T> {
	
	@Inject
	private EntityManager entityManager;
	
	private Class<T> clazz;

	public BaseRepository(Class<T> clazz){
		this.clazz = clazz; 
	}
	
	
	public EntityManager getEntityManager() {
		return entityManager;
	}
	
	public T find(Long id){
		return entityManager.find(clazz, id);
	}
	
	public void save(T obj){
		entityManager.persist(obj);
	}
	
	public void delete(T obj){
		entityManager.remove(obj);
	}
	
	public void update(T obj){
		entityManager.merge(obj);
	}
	
	
	public void deleteById(Long id){
		T obj = find(id);
		delete(obj);
	}
}
